package br.com.marcelo.reservasis.client.pagina.hospede;

import br.com.aroeirasoftware.gwtcomponents.client.components.ASFieldSize;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASMessagePanel;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASMessagePanel.ASMessageSeverity;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASOperation;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASPageSearch;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASSearcher;
import br.com.aroeirasoftware.gwtcomponents.client.components.ASTableColumn;
import br.com.aroeirasoftware.gwtcomponents.client.query.ASQuery;
import br.com.aroeirasoftware.gwtcomponents.client.query.ASQueryObject;
import br.com.aroeirasoftware.gwtcomponents.client.query.ASQueryResult;
import br.com.marcelo.reservasis.client.componentes.ReservaSISApplication;
import br.com.marcelo.reservasis.client.componentes.ReservaSISResources;
import br.com.marcelo.reservasis.client.i18n.ReservaSISI18N;
import br.com.marcelo.reservasis.client.services.hospede.HospedeUIService;
import br.com.marcelo.reservasis.domain.model.Hospede;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.lang.String;
import java.util.Map;

/**
 *
 * @author Marcelo Rodrigues
 */
public class HospedePesquisarPagina extends ASPageSearch<Hospede> {

    private ASQuery consulta;
    private ASSearcher<Hospede> pesquisador;

    public HospedePesquisarPagina() {
        super(null, ReservaSISApplication.getInstance(),
                ReservaSISI18N.messages.hospede(),
                ReservaSISResources.HOSPEDE(),
                new ASTableColumn[]{new ASTableColumn(0, ReservaSISI18N.messages.cpf(), "texto-centro", "texto-centro", "25%"),
                    new ASTableColumn(1, ReservaSISI18N.messages.nome(), "texto-centro", "texto-esquerda", "50%"),
                    new ASTableColumn(2, ReservaSISI18N.messages.telefone(), "texto-centro", "texto-centro", "25%")},
                new ASOperation[]{ASOperation.CRIACAO, ASOperation.ALTERACAO, ASOperation.EXCLUSAO, ASOperation.LEITURA});
    }

    public HospedePesquisarPagina(ASSearcher<Hospede> pesquisador) {
        super(null, ReservaSISApplication.getInstance(),
                ReservaSISI18N.messages.hospede(),
                ReservaSISResources.HOSPEDE(),
                new ASTableColumn[]{new ASTableColumn(0, ReservaSISI18N.messages.cpf(), "texto-centro", "texto-centro", "25%"),
                    new ASTableColumn(1, ReservaSISI18N.messages.nome(), "texto-centro", "texto-esquerda", "50%"),
                    new ASTableColumn(2, ReservaSISI18N.messages.telefone(), "texto-centro", "texto-centro", "25%")},
                new ASOperation[]{ASOperation.SELECAO});
        this.pesquisador = pesquisador;
    }

    @Override
    public void pesquisar(Map<String, Object> parametros) {
        ASQueryObject qo = new ASQueryObject(Hospede.class, "h");
        qo.addSelect(ASQueryObject.Expressions.as("h.idhospede", "idhospede")).
                addSelect(ASQueryObject.Expressions.as("h.nome", "nome")).
                addSelect(ASQueryObject.Expressions.as("h.cpf", "cpf")).
                addSelect(ASQueryObject.Expressions.as("h.telefone", "telefone"));

        qo.addWhere(ASQueryObject.Expressions.likeFull("h.nome", parametros.get("nome")));
        qo.addWhere(ASQueryObject.Expressions.eq("h.cpf", parametros.get("cpf")));

        consulta = qo.getConsulta(ASQueryObject.Expressions.count("h.idhospede", "idhospede"), true, 5);

        paginar(1);

    }

    @Override
    public void renderizarLinhaTabela(Hospede t) {
        addCelulaTabela(ReservaSISI18N.messages.cpf(), t.getCpf());
        addCelulaTabela(ReservaSISI18N.messages.nome(), t.getNome());
        addCelulaTabela(ReservaSISI18N.messages.telefone(), t.getTelefone());
    }

    @Override
    public void paginar(int pagina) {
        consulta.setPagina(pagina);

        HospedeUIService.Async.getInstance().getPorConsulta(consulta, new AsyncCallback<ASQueryResult<Hospede>>() {

            public void onFailure(Throwable caught) {
                ASMessagePanel.getInstance().exibirMensagem(
                        ReservaSISI18N.messages.msgErro(ReservaSISI18N.messages.gPesquisar(), caught),
                        ASMessagePanel.ASMessageSeverity.ERRO,
                        ASMessagePanel.ASMessageOption.OK, new Command() {

                    public void execute() {
                        liberarPesquisa();
                    }
                });
            }

            public void onSuccess(ASQueryResult<Hospede> result) {
                setResultadoPesquisa(result.getPagina(), result.getPaginas(), result.getLista());
            }
        });
    }

    @Override
    public void executar(Hospede t, String nomeOperacao) {
        if (nomeOperacao.equals(OPERACAO_CADASTRAR)) {
            new HospedeCriarAlterarPagina(this).abrir();
        } else if (nomeOperacao.equals(OPERACAO_DETALHAR)) {
        } else if (nomeOperacao.equals(OPERACAO_ALTERAR)) {
            new HospedeCriarAlterarPagina(this, t).abrir();
        } else if (nomeOperacao.equals(OPERACAO_EXCLUIR)) {
            HospedeUIService.Async.getInstance().excluir(t, new AsyncCallback<Void>() {

                @Override
                public void onFailure(Throwable caught) {
                    ASMessagePanel.getInstance().exibirMensagem(
                            ReservaSISI18N.messages.msgErro(ReservaSISI18N.messages.gExcluir(), caught),
                            ASMessagePanel.ASMessageSeverity.ERRO,
                            ASMessagePanel.ASMessageOption.OK, new Command() {

                        public void execute() {
                            liberarPesquisa();
                        }
                    });
                }

                @Override
                public void onSuccess(Void result) {
                    ASMessagePanel.getInstance().exibirMensagem(
                            ReservaSISI18N.messages.msgSucesso(
                            ReservaSISI18N.messages.hospede(),
                            "",
                            ReservaSISI18N.messages.gExcluido()),
                            ASMessageSeverity.SUCESSO, ASMessagePanel.ASMessageOption.OK);
                }
            });
        } else if (nomeOperacao.equals(OPERACAO_SELECIONAR)) {
            pesquisador.setValue(t);
            fechar();
        }

    }

    @Override
    public void configurarComponentes() {
        addCampoTexto("nome", ReservaSISI18N.messages.nome(), false, ASFieldSize.ENORME);
        addCampoTextoFormatado("cpf", ReservaSISI18N.messages.cpf(), "999.999.999-99", "999.999.999-99", "999.999.999-99", false, ASFieldSize.MEDIO);
    }

    @Override
    public void limpar() {
        super.limpar();
    }
}
